package concurrent;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: study2025
 * @description:
 * @author: liuChangFu
 * @data: 2025-02-10 16:34
 **/
public class WaitNotifyThread {

    Queue<String> queue = new LinkedList<>();

    public void put(String data){
        synchronized (this){
            queue.add(data);
            notify();
        }

    }

    public String take() throws InterruptedException {
        synchronized (this){
            while(queue.isEmpty()){
                wait();
            }
            return queue.remove();
        }
    }

    public static void main(String[] args) {
        WaitNotifyThread waitNotifyThread = new WaitNotifyThread();
        new Thread(()->{
            while (true) {
                waitNotifyThread.put("1");
            }
        }).start();

        new Thread(()->{
            try {
                while (true) {
                    System.out.println(waitNotifyThread.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
